package com.welsmann.framework.combiner;

import org.apache.log4j.BasicConfigurator;

import com.welsmann.framework.cache.memcached.MemcachedClient;
import com.welsmann.framework.cache.memcached.SockIOPool;

/**
 * Memcached缓存操作类
 * @author Welsmann
 *
 */
public class MemcachedUtil {
	
	private MemcachedClient client = null;
	private SockIOPool pool = SockIOPool.getInstance();
	
	
	public MemcachedUtil(){
		BasicConfigurator.configure();
		String[] servers = new String[]{"127.0.0.1:11211"};
		pool.setServers(servers);
		pool.setFailover(true);
		pool.setInitConn(10);
		pool.setMinConn(5);
		pool.setMaxConn(50);
		pool.setMaintSleep(30);
		pool.setNagle(false);
		pool.setSocketTO(3000);
		pool.setAliveCheck(true);
		pool.initialize();
		client = new MemcachedClient();
		com.welsmann.framework.cache.memcached.Logger.getLogger(MemcachedClient.class.getName())
			.setLevel(com.welsmann.framework.cache.memcached.Logger.LEVEL_FATAL);
	}
	
	public String put(String key, String value) {
		if (client == null) {
			return null;
		}
		client.set(key, value);
		return value;
	}
	
	public String get(String key) {
		if (client == null) {
			return null;
		}
		return client.keyExists(key) ? (String)client.get(key) : null;
	}
	
}
